package timbap.visualizer.impl.context.playlist;

import com.icl.saxon.TransformerFactoryImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xml.resolver.tools.CatalogResolver;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import timbap.visualizer.api.context.Configuration;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class ITunesLibraryParsingPopupPlaylistChooserImpl extends PopupPlaylistChooserImpl {

    private static final Log LOG = LogFactory.getLog(ITunesLibraryParsingPopupPlaylistChooserImpl.class);
    private static final String ITUNES_LIBRARY_PARSING_LOGFILE_PATH = "playlists/iTunesLibraryParsing.log";

    public ITunesLibraryParsingPopupPlaylistChooserImpl(Configuration cfg) {
        super(cfg);
    }
    
    @Override
    public File choosePlaylistFile() {

        if (getCfg().isITunesParsingEnabled()) {

			LOG.info("Transforming iTunes Library from " + getCfg().getITunesMusicLibrary() + " to M3U playlist files ...");
			TransformerFactoryImpl factory = new TransformerFactoryImpl();

			Source styleSource = new StreamSource(getCfg().getITunesToM3UStylesheetFile());

			try {
				Templates sheet = factory.newTemplates(styleSource);
				Transformer instance = sheet.newTransformer();
                Result result = new StreamResult(new File(ITUNES_LIBRARY_PARSING_LOGFILE_PATH));

                SAXSource tSource = new SAXSource(new InputSource(
                        new FileInputStream(getCfg().getITunesMusicLibrary())));
				EntityResolver resolver = new CatalogResolver();
				try {
					XMLReader tLocallyResolvingParser = XMLReaderFactory.createXMLReader();
					tLocallyResolvingParser.setEntityResolver(resolver);
					tSource.setXMLReader(tLocallyResolvingParser);
                    if (instance != null) {
                        instance.transform(tSource, result);
                    }
                } catch (SAXException e) {
					e.printStackTrace();
				} catch (TransformerException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (TransformerConfigurationException e) {
                e.printStackTrace();
            }

            LOG.info("Transformation successful.");
		}

        return super.choosePlaylistFile();
    }
}
